﻿'Copyright © Etheral Studios 2012
'File created by Nick nick@etheralstudios.com
'File has been edited by:
'Nick nick@etheralstudios.com

Public Class Voice

    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
    Dim Address As String = MainForm.Address

    Private Sub Voice_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        IP.Text = Address
        StopSpeak.Enabled = False
        Timer.Start()
    End Sub

    Private Sub Speak_Click(sender As System.Object, e As System.EventArgs) Handles Speak.Click
        Timer.Stop()
        Speak.Enabled = False
        StopSpeak.Enabled = True
        mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)
        mciSendString("record recsound", "", 0, 0)
    End Sub

    Private Sub StopSpeak_Click(sender As System.Object, e As System.EventArgs) Handles StopSpeak.Click
        Dim message As String = "1.0.1.0_NT\voice/media\" + MainForm.MyIP + "\" + Address + "\" + System.DateTime.Now
        Timer.Start()
        Speak.Enabled = True
        If My.Computer.FileSystem.DirectoryExists("C:\ProgramData\WiN\Server\htdocs\WiN\Voice\" + Address) Then
            mciSendString("save recsound C:\ProgramData\WiN\Server\htdocs\WiN\Voice\" + Address + "\mmsg.wav", "", 0, 0)
            mciSendString("close recsound", "", 0, 0)
            My.Computer.Audio.Stop()
            Dim myStreamWriter As System.IO.StreamWriter
            myStreamWriter = System.IO.File.CreateText("C:\ProgramData\WiN\Server\htdocs\WiN\Voice\" + Address + "\mmsg.wmsg")
            myStreamWriter.Write(message)
            myStreamWriter.Flush()
            myStreamWriter.Dispose()
        Else
            My.Computer.FileSystem.CreateDirectory("C:\ProgramData\WiN\Server\htdocs\WiN\Voice\" + Address)
            mciSendString("save recsound C:\ProgramData\WiN\Server\htdocs\WiN\Voice\" + Address + "\mmsg.wav", "", 0, 0)
            Dim myStreamWriter As System.IO.StreamWriter
            myStreamWriter = System.IO.File.CreateText("C:\ProgramData\WiN\Server\htdocs\WiN\Voice\" + Address + "\mmsg.wmsg")
            myStreamWriter.Write(message)
            myStreamWriter.Flush()
            myStreamWriter.Dispose()
            mciSendString("close recsound", "", 0, 0)
            My.Computer.Audio.Stop()
        End If
        StopSpeak.Enabled = False
    End Sub

    Private Sub Voice_FormClosed(sender As System.Object, e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        MainForm.Show()
    End Sub

    Private Sub Timer_Tick(sender As System.Object, e As System.EventArgs) Handles Timer.Tick
        If My.Computer.FileSystem.DirectoryExists("C:\ProgramData\WiN\Voice\" + Address) Then
            Try
                My.Computer.Network.DownloadFile("http://" + Address + "/WiN/Voice/" + MainForm.MyIP + "/mmsg.wmsg", "C:\ProgramData\WiN\Voice\" + Address + "\mmsg.wmsg")
                Dim wmsg_read() As String = IO.File.ReadAllLines("C:\ProgramData\WiN\Voice\" + Address + "\mmsg.wmsg")
                Dim firstLine As String = wmsg_read(0)
                Dim data() As String = firstLine.Split("\")
                Dim timestamp As DateTime = DateTime.Parse(data(4))
                'If timestamp > System.DateTime.Now Then
                My.Computer.Network.DownloadFile("http://" + Address + "/WiN/Voice/" + MainForm.MyIP + "/mmsg.wav", "C:\ProgramData\WiN\Voice\" + Address + "\mmsg.wav")
                My.Computer.Audio.Play("C:\ProgramData\WiN\Voice\" + Address + "\mmsg.wav", AudioPlayMode.Background)
                My.Computer.FileSystem.DeleteFile("C:\ProgramData\WiN\Voice\" + Address + "\mmsg.wmsg")
                My.Computer.FileSystem.DeleteFile("C:\ProgramData\WiN\Voice\" + Address + "\mmsg.wav")
                'Else
                'End If
            Catch ex As Exception
                'MessageBox.Show(Address + " is not online.")
            End Try
        Else
            My.Computer.FileSystem.CreateDirectory("C:\ProgramData\WiN\Voice\" + Address)
            Try
                My.Computer.Network.DownloadFile("http://" + Address + "/WiN/Voice/" + MainForm.MyIP + "/mmsg.wmsg", "C:\ProgramData\WiN\Voice\" + Address + "\mmsg.wmsg")
                Dim wmsg_read() As String = IO.File.ReadAllLines("C:\ProgramData\WiN\Voice\" + Address + "\mmsg.wmsg")
                Dim firstLine As String = wmsg_read(0)
                Dim data() As String = firstLine.Split("\")
                Dim timestamp As DateTime = DateTime.Parse(data(4))
                'If timestamp > System.DateTime.Now Then
                My.Computer.Network.DownloadFile("http://" + Address + "/WiN/Voice/" + MainForm.MyIP + "/mmsg.wav", "C:\ProgramData\WiN\Voice\" + Address + "\mmsg.wav")
                My.Computer.Audio.Play("C:\ProgramData\WiN\Voice\" + Address + "\mmsg.wav", AudioPlayMode.Background)
                'Else
                'End If
            Catch ex As Exception
                'MessageBox.Show(Address + " is not online.")
            End Try
        End If
    End Sub
End Class